package com.han.nettytestdemo.connector.netty.client;

import com.han.nettytestdemo.connector.netty.util.ByteUtil;

import java.util.List;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;
import io.netty.util.internal.StringUtil;

public class JsonDecoder extends MessageToMessageDecoder<ByteBuf> {

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out)
            throws Exception {
        int byteNum = buf.readableBytes();
        byte[] bytes = new byte[byteNum];
        buf.readBytes(bytes);
        String dataStr = ByteUtil.bytes2HexStr(bytes);
        if (!StringUtil.isNullOrEmpty(dataStr))
            out.add(dataStr);
    }
}
